//
//  HttpUtil.m
//  gdemm
//
//  Created by yongwu on 15/12/10.
//  Copyright © 2015年 goldensoft. All rights reserved.
//

#import "HttpUtil.h"

@implementation HttpUtil
+(NSString*)buildUrl:(NSString *)url WithParams:(NSMutableDictionary *)dict{

    NSMutableString *httpUrl = [NSMutableString stringWithFormat:@"%@",url];
    [httpUrl appendString:@"?"];
    
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        if (obj != nil) {
            [httpUrl appendString:key];
            [httpUrl appendString:@"="];
            [httpUrl appendString:obj];
            [httpUrl appendString:@"&"];
        }
    }];
    [httpUrl deleteCharactersInRange:NSMakeRange(httpUrl.length-1, 1)];
    NSString *encodeUrl = [httpUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    return encodeUrl ;
}

+(NSData*)httpRequestSyncWithUrl:(NSString *)url andParams:(NSMutableDictionary *)dict{
    NSString *encode = [self buildUrl:url WithParams:dict];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:encode]];
    [request startSynchronous];
    return [request responseData];
}

+ (BOOL)downloadTofileWithUrl:(NSString *)url withFileName:(NSString *)fileName{
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
    [request startSynchronous];
    NSData *data = [request responseData];
    if (data != nil) {
        id object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        if ([object isKindOfClass:[NSDictionary class]]) {
            if (object[@"result"] == nil) {
                
                [object writeToFile:fileName atomically:YES];
                return YES;
            }
        }if (object == nil) {
            [data writeToFile:fileName atomically:YES];
            return YES;
        }
    }
    return  NO;
}

/**
 *  下载策略文件文件到本地
 *
 *  @param url      请求URL
 *  @param fileName 本地文件地址
 *
 *  @return 
 */
+ (BOOL)downloadPoliciesTofileWithUrl:(NSString *)url withFileName:(NSString *)fileName{

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
    [request startSynchronous];
    NSData *data = [request responseData];
    if (data != nil) {
        NSString *jstr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        //HRLog(@"本地区域策略文件:%@",jstr);
        [jstr writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:nil];
        return YES;
    }
    return NO;
}


+ (NSDictionary *)downloadToMemory:(NSString *)url{
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
    [request startSynchronous];
    NSData *data = [request responseData];
    if (data == nil) {
        return nil;
    }
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    return dict;

}

+ (NSDictionary *)httpGet:(NSString *)url{
    NSDictionary *dict = [self downloadToMemory:url];
    if (dict != nil) {
        return dict;
    }
    return nil;
}
+ (NSDictionary *)postWithUrl:(NSString *)url withDict:(NSDictionary *)dict withType:(NSString *)type{
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
       NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
    [request setPostValue:jsonString forKey:type];
    [request startSynchronous];
    
    NSData *data = [request responseData];
    if (data == nil) {
        return nil;
    }
    NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    return jsonDict;
}
@end
